# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import tools
from openerp.osv import fields, osv


class freight_report(osv.osv):
    _name = "freight.report"
    _description = u"运费统计"
    _auto = False
    _rec_name = 'date'

    _columns = {
        'date': fields.datetime(u'发货单日期', readonly=True),
        'freight': fields.float(string=u'运费', readonly=True),
        'freight_department_id': fields.many2one('hr.department', u'运费承担部门', readonly=True),
        'carrier_id': fields.many2one('delivery.carrier', u'快递物流公司', readonly=True),
    }
    _order = 'date desc'

    def _select(self):
        select_str = """
             SELECT min(sp.id) as id,
                sp.freight_department_id as freight_department_id,
                sp.carrier_id as carrier_id,
                sp.date as date,
                sum(freight) as freight
        """
        return select_str

    def _from(self):
        from_str = """
                stock_picking sp
        """
        return from_str

    def _group_by(self):
        group_by_str = """
            GROUP BY
            sp.freight_department_id,
            sp.carrier_id,
            sp.date
        """
        return group_by_str

    def init(self, cr):
        # self._table = sale_report
        tools.drop_view_if_exists(cr, self._table)
        cr.execute("""CREATE or REPLACE VIEW %s as (
            %s
            FROM %s
            %s
            )""" % (self._table, self._select(), self._from(), self._group_by()))

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
